--#include "data\config\activityconfig\CrossActivity\CrossUnionWarCfg.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理

--活动是否开始
function IsCrossUnionWarStart()
	local sysVar = System.getDyanmicVar()
	if sysVar.CrossUnionWarStartFlag then
		return true
	end
	return false
end

--奖励是否可以领取
function IsCrossUnionWarCanGetAward()
	local sysVar = System.getDyanmicVar()
	if sysVar.CrossUnionWarAwardFlag then
		return true
	end
	return false
end

--活动开始
function CrossUnionWarStar()	
	local cfg = CrossUnionWarCfg
	
	gRankMgr_Clear(RANK_UNIONWAR_TOTAL)
	gRankMgr_Clear(RANK_UNIONWAR_PERSONAL)

	local sysVar = System.getDyanmicVar()
	sysVar.CrossUnionWarStartTime = System.getCurrMiniTime()
	sysVar.CrossUnionWarStartFlag = 1
	sysVar.CrossUnionWarAwardFlag = nil

	OpenCrossUnionWarRightPanelAll()

	System.broadcastTipmsg(Lang.ScriptTips.CrossUnionWar005,ttScreenCenter + ttChatWindow)
end


--活动结束
function CrossUnionWarEnd()	
	local sysVar = System.getDyanmicVar()
	if sysVar.CrossUnionWarStartFlag then
		--CrossUnionWarAward()
		sysVar.CrossUnionWarStartFlag = nil
		sysVar.CrossUnionWarAwardFlag = 1

		local nSceneId = CrossUnionWarCfg.unionTeams[1].EnterPos[1]
		local winIdx = GetWinCamp()
		if CrossUnionWarCfg.unionTeams[winIdx] then
			System.broadcastTipmsg(string.format(Lang.ScriptTips.CrossUnionWar006, CrossUnionWarCfg.unionTeams[winIdx].name),ttScreenCenter + ttChatWindow)
		else
			System.broadcastTipmsg(Lang.ScriptTips.CrossUnionWar011,ttScreenCenter + ttChatWindow)
		end

		local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
		if(playerList == nil)then
			return
		end
		for k,v in pairs(playerList)do	--踢出场景
			Actor.enterScene(v,unpack(CrossUnionWarCfg.ExitPos))	
			SendActivityLog(v, ACTIVITY_DEFINE_CROSS_UNION_WAR, ActLogStatus_Complete)
		end
	end
end

--失去物品
function CrossUnionWarDropItem( sysarg, itemId, itemCount )
    if not IsCrossUnionWarStart() then
        return 
    end

    if not IsCrossUnionWar(sysarg) then
        return
    end
    if itemCount > 0 then
		for idx = 1, #CrossUnionWarCfg.ExchangeItems do
	   		local cfg = CrossUnionWarCfg.ExchangeItems[idx]
	   		local num = Actor.getItemCount(sysarg, cfg.id)
	   		if num > 0 then
	   			return
	   		end
	   	end
        SetPropertyHeadItemValue(sysarg, 4, 0)  --删除头部item
    end
end

--获得物品
function CrossUnionWarPickItem( sysarg, itemId, itemCount )
    if IsCrossUnionWar(sysarg) and IsCrossUnionWarStart() then
        if itemCount > 0 then
            SetPropertyHeadItemValue(sysarg, 4, CrossUnionWarCfg.ShowTitleItemId)  --增加头部item
        end
    end
end

--上交水晶
function CrossUnionWarExchange(sysarg, npcId)
	if not IsCrossUnionWarStart() then
        return 
    end

    if not IsCrossUnionWar(sysarg) then
        return
    end

    local camp = CrossUnionWarGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not CrossUnionWarCfg.unionTeams[camp] then
   		return
   	end

   	if CrossUnionWarCfg.unionTeams[camp].npcId ~= npcId then
   		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar008, ttFlyTip)
   		return 
   	end
   	local AddScore = 0
   	for idx = 1, #CrossUnionWarCfg.ExchangeItems do
   		local cfg = CrossUnionWarCfg.ExchangeItems[idx]
   		local num = Actor.getItemCount(sysarg, cfg.id)
   		if num > 0 then
   			if not DoConsumeCond( sysarg, cfg.consume, num, LogIds.CrossUnionWarExchangeLogId, Lang.LogTips.CrossUnionWarExchange ) then
            	return 
        	end
        	AddScore = AddScore + num * cfg.score
   		end
   	end

   	if AddScore == 0 then
   		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar012, ttFlyTip)
		return
	end

   	local nActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID ) 

	local myBeforeIdx = 0
	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < CrossUnionWarCfg.rankLimit then			--未上榜
				myBeforeIdx = 0
			else
				myBeforeIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	--增加个人榜积分
	gRankMgr_AddValue(nActorId, RANK_UNIONWAR_PERSONAL, AddScore)

	--增加行会榜积分
	local unionName = CrossUnionWarCfg.unionTeams[camp].name
	gRankMgr_AddValue(camp, RANK_UNIONWAR_TOTAL, AddScore, unionName)

	local myCurIdx = 0
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < CrossUnionWarCfg.rankLimit then			--未上榜
				myCurIdx = 0
			else
				myCurIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.CrossUnionWar007,AddScore), ttTipmsgWindow)


	if myCurIdx == 1 or myCurIdx ~= myBeforeIdx then
		ChangeCrossUnionWarRightPanelAll()
	else
		ChangeCrossUnionWarRightPanel(sysarg)
	end
end

--打开面板
function CrossUnionWarOpenAwardsPanel(sysarg)
	Actor.openDialogs(sysarg, enLinkCrossUnionWarAwardWnd)
end

--进入活动
function CrossUnionWarEnter(sysarg)
	local zyVal = CrossUnionWarGetCamp(sysarg)
	if not zyVal then
		zyVal = CrossUnionWarGetMyCamp(sysarg)
		CrossUnionWarSetCamp(sysarg, zyVal)
	end

	local cfg = CrossUnionWarCfg.unionTeams[zyVal]
	if not cfg then
		return
	end

	Actor.enterScene(sysarg,unpack(cfg.EnterPos))

	Actor.setCampId(sysarg, zyVal)
	Actor.setPkMode(sysarg, fpZY)
	Actor.updateActorName(sysarg)


	OpenCrossUnionWarRightPanel(sysarg)
end

--获取阵营
function CrossUnionWarGetCamp(sysarg)
	local actorid = Actor.getActorId(sysarg)
	local value = gRankMgr_GetValueById(actorid, RANK_UNIONWAR_PERSONAL, 2) or 0
	if value and value > 0 then
		return value
	end
end

--计算阵营
function CrossUnionWarGetMyCamp(sysarg)
	local index = 0
	local curNum = 0
	for idx = 1, #CrossUnionWarCfg.unionTeams do
		local totalPower = gRankMgr_GetValueById(idx, RANK_UNIONWAR_TOTAL, 2) or 0
		--print("totalPower ="..totalPower)
		if curNum == 0 and index == 0 then
			index = idx
			curNum = totalPower
		elseif curNum > totalPower  then
			curNum = totalPower
			index = idx
		end
	end
	--print("index ="..index)
	return index
end

--设置阵营
function CrossUnionWarSetCamp(sysarg, index)
	local actorid = Actor.getActorId(sysarg)
	gRankMgr_SetRankEx(actorid, RANK_UNIONWAR_PERSONAL, 2, index)

	local power = Actor.getIntProperty(sysarg, PROP_ACTOR_BATTLE_POWER)
	local totalPower = gRankMgr_GetValueById(index, RANK_UNIONWAR_TOTAL, 2) or 0
	totalPower = totalPower + power
	--print("totalPower ="..totalPower)
	local unionName = CrossUnionWarCfg.unionTeams[index].name
	gRankMgr_SetRankEx(index, RANK_UNIONWAR_TOTAL, 2, totalPower, unionName)
end

--清除buff
function ClearCrossUnionWarBuff(sysarg)
    for k,v in ipairs(CrossUnionWarCfg.deadBuff) do
        for x,y in ipairs(v.buffId or {}) do
			Actor.delBuffById(sysarg, y)
		end
	end
end
 
--离开场景
function CrossUnionWarLeaveActivityHandler(sysarg)
	local sysVar = System.getDyanmicVar()
	Actor.setCampId(sysarg, 0)
	Actor.updateActorName(sysarg)
	Actor.setPkMode(sysarg, fpPeaceful)	
	CloseActivityRightPanel(sysarg)	
	ClearCrossUnionWarBuff(sysarg)
	SetPropertyHeadItemValue(sysarg, 4, 0)  --离开活动删除头部item

	CrossUnionWarClearBagItem(sysarg)
end

--离开活动场景
function CrossUnionWarLeaveActivity(sysarg)
	Actor.enterScene(sysarg,unpack(CrossUnionWarCfg.ExitPos))
end

--杀人加分 sysarg = 被杀者
function CrossUnionWarActorDeath( sysarg, killerPtr )
	--BaseFuc_Print("Enter CrossUnionWarActorDeath")
	if(sysarg == nil or killerPtr == nil)then
		return
	end

    if not IsCrossUnionWar(sysarg) then
        return
    end

	if Actor.isMonster(sysarg) then
		return
	end

	local myCamp = Actor.getIntProperty(sysarg, PROP_ACTOR_ZY)
	local killerCamp = Actor.getIntProperty(killerPtr, PROP_ACTOR_ZY)
	if myCamp == 0 or killerCamp == 0 or myCamp == killerCamp then
		return
	end

	ClearCrossUnionWarBuff(killerPtr)

	--local actorLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	--local actorCircle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
	local nActorId = Actor.getIntProperty( killerPtr, PROP_ENTITY_ID ) 

	local myBeforeIdx = 0
	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < CrossUnionWarCfg.rankLimit then			--未上榜
				myBeforeIdx = 0
			else
				myBeforeIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	local AddScore = 0
	for idx = 1, #CrossUnionWarCfg.killScore do
		local value = CrossUnionWarCfg.killScore[idx]
		if Actor.checkActorLevel(sysarg, value.level, value.circle)then
			AddScore = value.addScore
			--增加个人榜积分
			gRankMgr_AddValue(nActorId, RANK_UNIONWAR_PERSONAL, value.addScore)

			--增加行会榜积分
			
			local index = CrossUnionWarGetCamp(killerPtr)
			if index and index > 0 then
				local unionName = CrossUnionWarCfg.unionTeams[index].name
				gRankMgr_AddValue(index, RANK_UNIONWAR_TOTAL, value.addScore, unionName)
			end
			break
		end
	end

	if AddScore == 0 then
		return
	end

	local myCurIdx = 0
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < CrossUnionWarCfg.rankLimit then			--未上榜
				myCurIdx = 0
			else
				myCurIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end


	if myCurIdx == 1 or myCurIdx ~= myBeforeIdx then
		ChangeCrossUnionWarRightPanelAll()
	else
		ChangeCrossUnionWarRightPanel(killerPtr)
	end

	Actor.sendTipmsg(killerPtr, string.format(Lang.ScriptTips.CrossUnionWar007,AddScore), ttTipmsgWindow)

	local actorId = Actor.getActorId(sysarg)
	gRankMgr_AddValueEx(actorId, RANK_UNIONWAR_PERSONAL, 1, 1)
end

--复活
function CrossUnionWarRelive(sysarg, reliveType)
	if reliveType and reliveType == 2 then
		local zyVal = CrossUnionWarGetCamp(sysarg)

		local cfg = CrossUnionWarCfg.unionTeams[zyVal]
		if not cfg then
			return
		end

		Actor.enterScene(sysarg,unpack(cfg.EnterPos))
	end

	local actorId = Actor.getActorId(sysarg)
	local count = gRankMgr_GetValueById(actorId, RANK_UNIONWAR_PERSONAL, 1)  or 0
	
	for idx = 1, #CrossUnionWarCfg.deadBuff do
		local cfg = CrossUnionWarCfg.deadBuff[idx]
		if count == cfg.deadCount then
			for ii = 1, #cfg.buffId do
				Actor.addBuffById(sysarg, cfg.buffId[ii])
			end
			break
		end
	end
end

--领取奖励
function CrossUnionWarGetAward(sysarg, packet)
	local opType = DataPack.readByte(packet)
	CrossUnionWarGetAwardSub(sysarg, opType)
end

--领取奖励
function CrossUnionWarGetAwardSub(sysarg, opType)
	if not IsCrossUnionWarCanGetAward() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar010, ttFlyTip)
		return
	end
	local camp = CrossUnionWarGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not CrossUnionWarCfg.unionTeams[camp] then
   		return
   	end

   	if opType == 1 then
   		local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
		local rankingPtr = Ranking.getRanking( rankName )
		if (rankingPtr == nil) then
			return
		end

	   	local rankLimit = CrossUnionWarCfg.rankLimit
		local myId = Actor.getActorId(sysarg)
		local myScore 	= 0
		local myIdx 	= 0
		local myState   = 0
		local item = Ranking.getItemPtrFromId( rankingPtr, myId)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < rankLimit then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
				myState = tonumber(Ranking.getSub(item, 3)) or 0
			end

			local myGetFlag = false
			if myState == 0 and myIdx > 0 then
				for idx = 1, #CrossUnionWarCfg.Awards do
					local cfg = CrossUnionWarCfg.Awards[idx]
					if cfg then
						if cfg.condition[1] <= myIdx and cfg.condition[2] >= myIdx  then
							myGetFlag = true
							break
						end
					end
				end
			end

			if myState == 1 or myGetFlag == true then
				local myAwards = CrossUnionWarGetAwardIdx(sysarg, myIdx)
				if IsBagGridEnough(sysarg, myAwards) then
					myState = 2
					Ranking.setSub(item, 3, myState)
					GiveCommonAward(sysarg, myAwards, LogIds.CrossUnionWarPersonalLogId, Lang.LogTips.CrossUnionWarPersonal)

					CrossUnionWarGetAwardData(sysarg)
				else	
					Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
				end
			else
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar009, ttFlyTip)
			end

		end
   	elseif opType == 2 then
   		local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
		local rankingPtr = Ranking.getRanking( rankName )
		if (rankingPtr == nil) then
			return
		end

	   	local rankLimit = CrossUnionWarCfg.rankLimit
		local myId = Actor.getActorId(sysarg)
		
		local campState = 0
		local item = Ranking.getItemPtrFromId( rankingPtr, myId)
		if item then
			campState = tonumber(Ranking.getSub(item, 4)) or 0

			if campState == 1 or (campState == 0 and GetWinCamp() == camp) then
				local campAwards = CrossUnionWarCfg.winnerAwards
				if IsBagGridEnough(sysarg, campAwards) then
					campState = 2
					Ranking.setSub(item, 4, campState)
					GiveCommonAward(sysarg, campAwards, LogIds.CrossUnionWarCampLogId, Lang.LogTips.CrossUnionWarCamp)

					CrossUnionWarGetAwardData(sysarg)
				else	
					Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
				end
			else
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar009, ttFlyTip)
			end
		end
   	end
end

--获取胜利方
function GetWinCamp()
	if not IsCrossUnionWarCanGetAward() then
		return 0
	end
	local winIdx = 0
	local winScore = 0
	local winCount = 0
	for idx = 1, #CrossUnionWarCfg.unionTeams do
		local cfg = CrossUnionWarCfg.unionTeams[idx]
		local campScore = gRankMgr_GetValue(idx, RANK_UNIONWAR_TOTAL)
		if winIdx == 0 then
			winIdx = idx
			winScore = campScore
			winCount = 1
		elseif winScore < campScore then
			winIdx = idx
			winScore = campScore
			winCount = 1
		elseif winScore == campScore then
			winCount = winCount + 1
		end
	end
	if winCount <= 1 then
		return winIdx
	end
	return 0
end

--获取奖励信息
function CrossUnionWarGetAwardData(sysarg)
	local camp = CrossUnionWarGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not CrossUnionWarCfg.unionTeams[camp] then
   		return
   	end

   	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if (rankingPtr == nil) then
		return
	end

	local rankLimit = CrossUnionWarCfg.rankLimit
	local myId = Actor.getActorId(sysarg)
	local myScore 	= 0
	local myIdx 	= 0
	local myState   = 0
	local campState = 0
	local item = Ranking.getItemPtrFromId( rankingPtr, myId)
	if item then
		myScore = Ranking.getPoint(item) or 0
		if myScore < rankLimit then			--未上榜
			myIdx = 0
		else
			myIdx = Ranking.getIndexFromPtr(item) + 1
			myState = tonumber(Ranking.getSub(item, 3)) or 0
		end
		campState = tonumber(Ranking.getSub(item, 4)) or 0

		if IsCrossUnionWarCanGetAward() and myState == 0 and myIdx > 0 then
			for idx = 1, #CrossUnionWarCfg.Awards do
				local cfg = CrossUnionWarCfg.Awards[idx]
				if cfg then
					if cfg.condition[1] <= myIdx and cfg.condition[2] >= myIdx  then
						myState = 1
						Ranking.setSub(item, 3, myState)
						break
					end
				end
			end
		end

		if IsCrossUnionWarCanGetAward() and campState == 0 and GetWinCamp() == camp then
			campState = 1
			Ranking.setSub(item, 4, campState)
		end
	end	

	local campScore = gRankMgr_GetValue(camp, RANK_UNIONWAR_TOTAL)

	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetCrossUnionWarAwardData)
	if (pack == nil) then
		return
	end
	--print("myIdx = "..myIdx)	
	DataPack.writeShort(pack, myIdx)	
	--print("myScore = "..myScore)	
	DataPack.writeInt(pack, myScore)
	--print("myState = "..myState)	
	DataPack.writeByte(pack, myState)
	--print("camp = "..camp)	
	DataPack.writeByte(pack, camp)
	--print("campScore = "..campScore)	
	DataPack.writeInt(pack, campScore)
	--print("campState = "..campState)	
	DataPack.writeByte(pack, campState)

	DataPack.flush(pack)
end

--前端请求获得排行榜
function CrossUnionWarGetRank(sysarg)
	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if (rankingPtr == nil) then
		return
	end
	local num = CrossUnionWarCfg.FinialRankShowNum
	local itemNum = Ranking.getRankItemCount(rankingPtr)
	if num < Ranking.getRankItemCount(rankingPtr) then
		itemNum = num
	end
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetCrossUnionWarRank)
	if (pack == nil) then
		return
	end
	local rankLimit = CrossUnionWarCfg.rankLimit
	--我的数据
	local myId = Actor.getActorId(sysarg)
	local myScore 	= 0
	local myIdx 	= 0
	local item = Ranking.getItemPtrFromId( rankingPtr, myId)
	if item then
		myScore = Ranking.getPoint(item) or 0
		if myScore < rankLimit then			--未上榜
			myIdx = 0
		else
			myIdx = Ranking.getIndexFromPtr(item) + 1
		end
	end	

	--print("myIdx = "..myIdx)
	--print("myScore = "..myScore)
	local campLen = #CrossUnionWarCfg.unionTeams
	DataPack.writeByte(pack, campLen)
	for ii = 1, campLen do
		local campScore = gRankMgr_GetValue(ii, RANK_UNIONWAR_TOTAL)
		--print("ii = "..ii)
		DataPack.writeByte(pack, ii)
		--print("campScore = "..campScore)
		DataPack.writeInt(pack, campScore)
	end

	--print("myIdx = "..myIdx)
	DataPack.writeShort(pack, myIdx)
	--print("myScore = "..myScore)	
	DataPack.writeInt(pack, myScore)

	local camp = CrossUnionWarGetCamp(sysarg)

	DataPack.writeByte(pack, camp)

	local sendNum = true
	local sendDataNum 
	for idx=itemNum,1, -1 do
		local rankItem = Ranking.getItemFromIndex(rankingPtr, idx-1)
		if rankItem then
			local value = Ranking.getPoint(rankItem)
			if value >= rankLimit then
				if sendNum then
					if idx > CrossUnionWarCfg.FinialRankShowNum then
						sendDataNum = CrossUnionWarCfg.FinialRankShowNum
					else
						sendDataNum = idx
					end
					DataPack.writeByte(pack, sendDataNum)
					--print("sendDataNum = "..sendDataNum)
					sendNum = false
				end
				if idx <= sendDataNum then
					local name = Ranking.getSub(rankItem, 0)
					DataPack.writeString(pack, name)	
					--print("name = "..name)		
					DataPack.writeInt(pack, value)
					--print("value = "..value)
					DataPack.writeByte(pack, tonumber(Ranking.getSub(rankItem, 2)) )
					--print("tonumber(Ranking.getSub(rankItem, 2)) = "..tonumber(Ranking.getSub(rankItem, 2)))
				end
			end
		end
	end

	if sendNum then
		--print("sendDataNum = 0")
		DataPack.writeByte(pack, 0)
	end

	DataPack.flush(pack)
end

--根据排名获得活动奖励位置
function CrossUnionWarGetAwardIdx(sysarg, idx)
	local fightingRewards = {}
	if(1 <= idx)then
		for k,v in pairs(CrossUnionWarCfg.Awards)do
			if(v.condition[1] <= idx and idx <= v.condition[2])then
				fightingRewards = v.award
				break
			end
		end
	end
	local actorLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)	
	return TransAwardsByType(fightingRewards, actorLevel)
end

--活动判断
function IsCrossUnionWar(sysarg)
	local mySceneId =  Actor.getSceneId(sysarg)
	if(mySceneId == CrossUnionWarCfg.unionTeams[1].EnterPos[1])then
		return true
	end
	return false
end


--打开所有人的左侧面板
function OpenCrossUnionWarRightPanelAll()
	local playerList = LuaHelp.getSceneActorListById( CrossUnionWarCfg.unionTeams[1].EnterPos[1] ) or {}
	for i,v in ipairs(playerList) do
		OpenCrossUnionWarRightPanel(v)
	end
end

--改变所有人的左侧面板
function ChangeCrossUnionWarRightPanelAll(sysarg)
	local playerList = LuaHelp.getSceneActorListById( CrossUnionWarCfg.unionTeams[1].EnterPos[1] ) or {}
	for i,v in ipairs(playerList) do
		if sysarg and sysarg == v then
		else
			ChangeCrossUnionWarRightPanel(v)
		end
	end
end

--打开左侧面板
function OpenCrossUnionWarRightPanel(sysarg)

	local panelInfo = {}
	
	panelInfo.sts = 1
	
	panelInfo.title = Lang.ScriptTips.CrossUnionWarPanel001

	local sysVar = System.getDyanmicVar()

	panelInfo.timeTitle = Lang.ScriptTips.CrossUnionWarPanel002
	panelInfo.restTime = sysVar.CrossUnionWarStartTime + CrossUnionWarCfg.Time - System.getCurrMiniTime()

	panelInfo.contentTitle = ""

	local myScore 	= 0
	local myIdx 	= 0
	local firstScore = 0
	local firstName = ""
	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankLimit = CrossUnionWarCfg.rankLimit

		local firstItem = Ranking.getItemFromIndex(ranking, 0)
		if firstItem then
			firstName = Ranking.getSub(firstItem, 0)
			firstScore = Ranking.getPoint(firstItem)
			if firstScore < rankLimit then
				firstName = ""
				firstScore = 0
			end
		end

		local myId = Actor.getActorId(sysarg)
		local item = Ranking.getItemPtrFromId( ranking, myId)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < rankLimit then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end
	end

	local camp = CrossUnionWarGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not CrossUnionWarCfg.unionTeams[camp] then
   		return
   	end


	panelInfo.contents = {}
	if firstScore == 0 then
		panelInfo.contents[1] = Lang.ScriptTips.CrossUnionWarPanel007..Lang.ScriptTips.CrossUnionWarPanel009
	else
		panelInfo.contents[1] = Lang.ScriptTips.CrossUnionWarPanel007..firstName
	end
	panelInfo.contents[2] = string.format(Lang.ScriptTips.CrossUnionWarPanel008, firstScore)
	panelInfo.contents[3] = string.format(Lang.ScriptTips.CrossUnionWarPanel006, CrossUnionWarCfg.unionTeams[camp].name)
	if myIdx == 0 then
		panelInfo.contents[4] = Lang.ScriptTips.CrossUnionWarPanel003..Lang.ScriptTips.CrossUnionWarPanel005
	else
		panelInfo.contents[4] = Lang.ScriptTips.CrossUnionWarPanel003..myIdx
	end
	
	panelInfo.contents[5] = string.format(Lang.ScriptTips.CrossUnionWarPanel004,myScore)
	
	panelInfo.awardTitle = ""
	panelInfo.awards = {}

	panelInfo.buttons = {1, 4, 8}

	local zyVal = CrossUnionWarGetCamp(sysarg)
	if not zyVal then
		zyVal = CrossUnionWarGetMyCamp(sysarg)
	end

	panelInfo.subActivityId = zyVal

	local panelType = 2
	local activityId = ACTIVITY_DEFINE_CROSS_UNION_WAR
	OpenActivityRightPanel( sysarg, panelType, activityId, panelInfo )

end

--左侧面板改变
function ChangeCrossUnionWarRightPanel(sysarg)

	local panelInfo = {}

	panelInfo.sts = 1

	panelInfo.contentTitle = ""

	local myScore 	= 0
	local myIdx 	= 0
	local firstScore = 0
	local firstName = ""
	local rankName = RankList[RANK_UNIONWAR_PERSONAL].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankLimit = CrossUnionWarCfg.rankLimit

		local firstItem = Ranking.getItemFromIndex(ranking, 0)
		if firstItem then
			firstName = Ranking.getSub(firstItem, 0)
			firstScore = Ranking.getPoint(firstItem)
			if firstScore < rankLimit then
				firstName = ""
				firstScore = 0
			end
		end

		local myId = Actor.getActorId(sysarg)
		local item = Ranking.getItemPtrFromId( ranking, myId)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < rankLimit then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end
	end

	local camp = CrossUnionWarGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not CrossUnionWarCfg.unionTeams[camp] then
   		return
   	end


	panelInfo.contents = {}
	if firstScore == 0 then
		panelInfo.contents[1] = Lang.ScriptTips.CrossUnionWarPanel007..Lang.ScriptTips.CrossUnionWarPanel009
	else
		panelInfo.contents[1] = Lang.ScriptTips.CrossUnionWarPanel007..firstName
	end
	panelInfo.contents[2] = string.format(Lang.ScriptTips.CrossUnionWarPanel008, firstScore)
	panelInfo.contents[3] = string.format(Lang.ScriptTips.CrossUnionWarPanel006, CrossUnionWarCfg.unionTeams[camp].name)
	if myIdx == 0 then
		panelInfo.contents[4] = Lang.ScriptTips.CrossUnionWarPanel003..Lang.ScriptTips.CrossUnionWarPanel005
	else
		panelInfo.contents[4] = Lang.ScriptTips.CrossUnionWarPanel003..myIdx
	end
	
	panelInfo.contents[5] = string.format(Lang.ScriptTips.CrossUnionWarPanel004,myScore)
	

	panelInfo.buttons = {1, 4, 8}

	ChangeActivityRightPanel( sysarg, panelInfo )
end

function CrossUnionWarClearBagItem(sysarg)
	for idx = 1, #CrossUnionWarCfg.ExchangeItems do
		local cfg = CrossUnionWarCfg.ExchangeItems[idx]
		RemoveActorItemInBag( sysarg, cfg.id, LogIds.CrossUnionWarClearBagLogId, Lang.LogTips.CrossUnionWarClearBag )	
	end
    Actor.sendTipmsg(sysarg, Lang.ScriptTips.CrossUnionWar013, ttFlyTip)
end


